var convert3gpToAvi=
{
	title:'Convert 3gp files to Avi format',
	createUI:function()
	{
		var ret=$('<div>'+
			'Video files<br>'+
			'<table id="tbl3gpFiles">'+
			'<thead><tr><th>Filename</th><th>Original Size</tr><th>Avi Size</th></thead><tbody></tbody>'+
			'</table>'+
			'<button id="btnRefresh3gpFiles">Refresh</button>'+
			'<button id="btnConvertAll3gpFiles">Convert All</button>'+
			'</div>');
		return ret;
	},
	postCreateUI:function()
	{
		$('#txtAlbumPath').change(function(){
			convert3gpToAvi.refresh();
		});
		$('#btnConvertAll3gpFiles').click(function(){
			convert3gpToAvi.convertAll();
		});
		$('#btnRefresh3gpFiles').click(function(){
			convert3gpToAvi.refresh();
		});
		
	},
	refresh:function(){
		
		$('#tbl3gpFiles tbody tr').remove();
		
		var folderObj;
		try{
			folderObj=File.objFSO.GetFolder($('#txtAlbumPath').val());
		}catch(e){
			return;
		}
		
		var fc = new Enumerator(folderObj.files);
		for (; !fc.atEnd(); fc.moveNext())
		{
			var filePath=''+fc.item();
			var fileName=filePath.substring(filePath.lastIndexOf('\\')+1);
			
			if(!/.3gp/i.exec(fileName))
				continue;

			var itemTemplate='<tr><td class="tdFileName"></td><td class="tdOriginalSize"></td><td class="tdCompressedSize"></td></tr>';
			

			var newRow=$(itemTemplate);
			newRow.children('.tdFileName').text(fileName);
			newRow.children('.tdOriginalSize').text(File.objFSO.GetFile(filePath).Size);
			newRow.get(0).info=
				{
					originalFilePath:filePath
				};
			
			$('#tbl3gpFiles tbody').append(newRow);
		}
		
	},
	convertAll:function(){
		$('#tbl3gpFiles tbody tr').each(function(){
			var info=$(this).context.info;

			{
				//compress the file
				
				//calculate the compressed file path
				var p=info.originalFilePath.lastIndexOf('.');
				var convertedPath=info.originalFilePath.substr(0,p)+'.avi';
				
				var cmd=Environment.binPath+'\\ffmpeg\\ffmpeg.exe -i "'+info.originalFilePath+
					'" -f avi -b 800 -acodec mp3 "'+ convertedPath+'"';
				var oExec=Shell.obj.Exec(cmd);
				var out=oExec.StdOut.ReadAll();
				var err=oExec.StdErr.ReadAll();
				
				File.objFSO.MoveFile(info.originalFilePath,info.originalFilePath+'.original');
							
			}
		});
		convert3gpToAvi.refresh();
	}
	
};
	
stepManager.add(convert3gpToAvi);
